package com.datastructure.test.childofbinarytree;

public class ChildOfBinaryTree {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(8);
        treeNode.left=new TreeNode(8);
        treeNode.right=new TreeNode(7);
        treeNode.left.left=new TreeNode(9);
        treeNode.left.right=new TreeNode(2);
        TreeNode treeNode1 = new TreeNode(8);
        treeNode1.left=new TreeNode(9);
        treeNode1.right=new TreeNode(2);
        ChildOfBinaryTree childOfBinaryTree = new ChildOfBinaryTree();
        boolean b = childOfBinaryTree.HasSubtree(treeNode, treeNode1);
        System.out.println(b);
    }

    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root2==null){
            return false;
        }
        if(root1==null){
            return false;
        }
        return computeIsContains(root1,root2)||HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);
    }

    private boolean computeIsContains(TreeNode root1, TreeNode root2) {
        if(root1 == null&&root2==null){
            return true;
        }
        if(root2 != null&&root1==null){
            return false;
        }
        if(root2 == null){
            return true;
        }
        return root1.val == root2.val && computeIsContains(root1.left, root2.left) && computeIsContains(root1.right, root2.right);
    }
}
class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
